In [1]:
import os
import folium
import json

print(folium.__version__)


0.2.1

zoom_start's bigger value means zoom in folium.Map(location=[x,x]) creates a map centred on x,x


In [2]:
gungahlin = folium.Map(location=[-35.187746,149.1201253],zoom_start=11)

popup is a small flag on the map


In [3]:
home_coordinate=[-35.1599026,149.0961]
home_marker=folium.Marker(home_coordinate,popup="jun's house")
home_marker.add_to(gungahlin)


Out[3]:
<folium.map.Marker at 0x7fa41519d1d0>

In [4]:
gungahlin


Out[4]:

In [5]:
geojson_file='./data/canberrageo.json'
geojson_layer = folium.GeoJson(open(geojson_file), name='geojson')   
geojson_layer.add_to(gungahlin)


Out[5]:
<folium.features.GeoJson at 0x7fa4151ba5f8>

canberrageo.json是关于堪培拉的地理信息geojson文件,它是featurecollection,以下是它的部分信息 { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "name": "Acton", "cartodb_id": 0, "created_at": "2013-11-26", "updated_at": "2013-11-26" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 149.098343, -35.291985 ], [ 149.098316, -35.291929 ], [ 149.098287, -35.291862 ], [ 149.098298, -35.291791 ], ........ 从这里可以发现,它就是把各个区的边界坐标收集起来放在geometry属性里


In [6]:
gungahlin


Out[6]:

In [7]:
path='./data/canberra_school.json'

json是方便于网络传输的编码 解码json,即load(xxx)就是将json文件提取出来弄成字典 可以同json_dict.keys()来知道该字典的关键词 一层一层剥洋葱,json就是字典套字典 底下这个函数提取json中关于各个学校的经度纬度


In [8]:
def parse_json(path):
    fp=open(path)
    json_dict=json.load(fp)
    datas=json_dict['data']
    for data in datas:
        [x,y]=data[-1][1:3]
        if type(x) is str:
            x=float(x)
            y=float(y)
            yield [x,y]
    fp.close()

In [9]:
school_geos=list(parse_json(path))

In [10]:
school_geos


Out[10]:
[[-35.200461, 149.141327],
 [-35.428716, 149.089497],
 [-35.341633, 149.076243],
 [-35.329016, 149.150698],
 [-35.261994, 149.120736],
 [-35.232767, 149.038363],
 [-35.163986, 149.129705],
 [-35.418155, 149.122047],
 [-35.181275, 149.100262],
 [-35.1975306, 149.1516423],
 [-35.227532, 149.021001],
 [-35.393842, 149.064431],
 [-35.394992, 149.086363],
 [-35.340387, 149.088676],
 [-35.249, 149.153],
 [-35.403008, 149.09717],
 [-35.185515, 149.131357],
 [-35.245207, 149.033634],
 [-35.238771, 149.073894],
 [-35.4132, 149.067934],
 [-35.212194, 149.059163],
 [-35.336028, 149.147301],
 [-35.273729, 149.139632],
 [-35.254797, 149.145509],
 [-35.163986, 149.129705],
 [-35.255999, 149.079914],
 [-35.355017, 149.059719],
 [-35.431846, 149.082604],
 [-35.438412, 149.109254],
 [-35.290271, 149.156099],
 [-35.356878, 149.043079],
 [-35.464883, 149.098449],
 [-35.322603, 149.042848],
 [-35.198682, 149.036203],
 [-35.418155, 149.122047],
 [-35.331662, 149.079926],
 [-35.334219, 149.033656],
 [-35.20707, 149.075906],
 [-35.404286, 149.118175],
 [-35.378739, 149.107114],
 [-35.226519, 149.052092],
 [-35.315084, 149.126171],
 [-35.200461, 149.141327],
 [-35.191795, 149.043375],
 [-35.344681, 149.103287],
 [-35.419488, 149.135582],
 [-35.213717, 149.094449],
 [-35.181275, 149.100262],
 [-35.455517, 149.086978],
 [-35.413608, 149.110565],
 [-35.1975306, 149.15164],
 [-35.250284, 149.034593],
 [-35.332248, 149.093456],
 [-35.428716, 149.089497],
 [-35.135012, 150.703614],
 [-35.224835, 149.11404],
 [-35.226971, 149.023361],
 [-35.220157, 149.034298],
 [-35.25148, 149.124974],
 [-35.341633, 149.076243],
 [-35.213112, 149.012349],
 [-35.250069, 149.059393],
 [-35.238509, 149.15501],
 [-35.235115, 149.102261],
 [-35.358505, 149.097655],
 [-35.213295, 149.066961],
 [-35.414879, 149.089411],
 [-35.208737, 149.055621],
 [-35.393842, 149.064431],
 [-35.329016, 149.150698],
 [-35.161141, 149.139812],
 [-35.169196, 149.111784],
 [-35.261994, 149.120736],
 [-35.1975, 149.120303],
 [-35.339741, 149.132893],
 [-35.428859, 149.113943],
 [-35.232767, 149.038363],
 [-35.378318, 149.064261],
 [-35.313804, 149.134385],
 [-35.447667, 149.12396],
 [-35.370046, 149.090949],
 [-35.264995, 149.126081],
 [-35.393069, 149.095898],
 [-35.394992, 149.086363],
 [-35.251826, 149.047137],
 [-35.306948, 149.103234],
 [-35.323849, 149.094282],
 [-35.163986, 149.129705],
 [-35.246957, 149.039298],
 [-35.441052, 149.117635],
 [-35.278101, 149.147123],
 [-35.251302, 149.074517],
 [-35.418155, 149.122047],
 [-35.181275, 149.100262],
 [-35.1975306, 149.15164],
 [-35.225083, 149.1012],
 [-35.227532, 149.021001],
 [-35.460389, 149.09507],
 [-35.252379, 149.130653],
 [-35.21254, 149.059794],
 [-35.363475, 149.089434],
 [-35.355429, 149.05436],
 [-35.393842, 149.064431],
 [-35.313804, 149.134385],
 [-35.395277, 149.085588],
 [-35.248452, 149.154143],
 [-35.350801, 149.049754],
 [-35.444729, 148.959975],
 [-35.265224, 149.112366],
 [-35.224913, 149.02911],
 [-35.34513, 149.10078],
 [-35.225083, 149.1012],
 [-35.337447, 149.09402],
 [-35.363475, 149.089434],
 [-35.264995, 149.126081],
 [-35.326599, 149.092398],
 [-35.254797, 149.145509],
 [-35.332248, 149.093456],
 [-35.249, 149.153],
 [-35.19876, 149.03622],
 [-35.39282, 149.09216],
 [-35.1975, 149.120303],
 [-35.249947, 149.162514],
 [-35.249003, 149.127639],
 [-35.188529, 149.123672],
 [-35.356475, 149.097963],
 [-35.31699, 149.115115],
 [-35.312126, 149.110759],
 [-35.330919, 149.124674],
 [-35.332344, 149.124691],
 [-35.332344, 149.124691],
 [-35.285316, 149.147091],
 [-35.332247, 149.051068],
 [-35.439222, 149.090709],
 [-35.253518, 149.138695],
 [-35.253355, 149.142818],
 [-35.166843, 149.123922],
 [-35.410301, 149.116552],
 [-35.180246, 149.101809],
 [-35.327679, 149.081951],
 [-35.240588, 149.15037],
 [-35.357959, 149.088697],
 [-35.266525, 149.137037],
 [-35.327858, 149.058949],
 [-35.243503, 149.089323],
 [-35.24641, 149.154682],
 [-35.366936, 149.084391],
 [-35.394049, 149.080086],
 [-35.338847, 149.128788],
 [-35.33483, 149.148034],
 [-35.45791, 149.096766],
 [-35.323929, 149.14765],
 [-35.32282, 149.144242],
 [-35.440799, 149.119593],
 [-35.223979, 149.040615],
 [-35.225664, 149.041696],
 [-35.351057, 149.055559],
 [-35.259648, 149.126375],
 [-35.333941, 149.049819],
 [-35.404923, 149.089258],
 [-35.404923, 149.089258],
 [-35.240755, 149.041555],
 [-35.223474, 149.109707],
 [-35.213525, 149.061289],
 [-35.339376, 149.092732],
 [-35.205121, 149.029638],
 [-35.286717, 149.156836],
 [-35.382449, 149.051977],
 [-35.254993, 149.076661],
 [-35.380778, 149.035968],
 [-35.408265, 149.086051]]

In [11]:
for item in school_geos:
    school_marker=folium.Marker(item,popup="school")
    school_marker.add_to(gungahlin)

以下这个运用cluster,地图显得非常简洁


In [13]:
gungahlin_cluster = folium.Map(location=[-35.187746,149.1201253],zoom_start=11) 
marker_cluster = folium.MarkerCluster().add_to(gungahlin_cluster)    
for item in school_geos:
    school_marker=folium.Marker(item,popup="school")
    school_marker.add_to(marker_cluster)
gungahlin_cluster


Out[13]:

In [ ]: